#pragma once

#include<iostream>

using namespace std;

namespace key
{   
	template<class K>
	struct BSTreeNode
	{
		BSTreeNode<K>* _left;
		BSTreeNode<K>* _right;
		K _key;
		BSTreeNode(const K& key)
			:_left(nullptr), _right(nullptr), _key(key)
		{ }
	};

	template<class K>
	class BSTree
	{
		using Node = BSTreeNode<K>;
	public:

		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}

			if ( _root->_key == key)
				return false;

			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
					return false;
			}
			cur = new Node(key);
			if (parent->_key > key)
				parent->_left = cur;
			else
				parent->_right = cur;
				
			return true;
		}

		void InOrder()
		{
			Inorder(_root);
		}

		bool Find(const K& key)
		{
			if (_root == nullptr)
				return false;

			if (_root->_key == key)
				return true;

			Node* cur = _root;

			while (cur)
			{
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else
					return true;
			}

			return false;
		}

		bool Erase(const K& key)
		{
			if (_root == nullptr)
				return false;

			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (parent == nullptr)
							_root = cur->_right;
						else
						{
							if (cur == parent->_left)
								parent->_left = cur->_right;
							else
								parent->_right = cur->_right;
						}
						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						if (parent == nullptr)
							_root = cur->_left;
						else
						{
							if (cur == parent->_left)
								parent->_left = cur->_left;
							else
								parent->_right = cur->_left;
						}
						delete cur;
						return true;
					}
					else
					{
						Node* replace = cur->_right;
						Node* replaceparent = cur;
						while (replace->_left)
						{
							replaceparent = replace;
							replace = replace->_left;
						}
						cur->_key = replace->_key;
						if (replace == replaceparent->_left)
							replaceparent->_left = replace->_right;
						else
							replaceparent->_right = replace->_right;
						delete replace;
						return true;
					}
				}
			}

			return false;
		}

	private:
		void Inorder(Node* root)
		{
			if (root == nullptr)
				return;
			Inorder(root->_left);
			cout << root->_key << ' ';
			Inorder(root->_right);
		}

	protected:

		Node* _root = nullptr;
	};
}

namespace key_value
{
	template<class K, class V>
	struct BSTreeNode
	{
		BSTreeNode<K, V>* _left;
		BSTreeNode<K, V>* _right;
		K _key;
		V _value;
		BSTreeNode(const K& key,const V& value)
			:_left(nullptr), _right(nullptr), _key(key),_value(value)
		{ }
	};

	template<class K, class V>
	class BSTree
	{
		using Node = BSTreeNode<K, V>;
	public:

		bool Insert(const K& key, const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key, value);
				return true;
			}

			if (_root->_key == key)
				return false;

			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
					return false;
			}
			cur = new Node(key, value);
			if (parent->_key > key)
				parent->_left = cur;
			else
				parent->_right = cur;

			return true;
		}

		void InOrder()
		{
			Inorder(_root);
		}

		Node* Find(const K& key)
		{
			if (_root == nullptr)
				return nullptr;

			if (_root->_key == key)
				return _root;

			Node* cur = _root;

			while (cur)
			{
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else
					return cur;
			}

			return nullptr;
		}

		bool Erase(const K& key)
		{
			if (_root == nullptr)
				return false;

			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (parent == nullptr)
							_root = cur->_right;
						else
						{
							if (cur == parent->_left)
								parent->_left = cur->_right;
							else
								parent->_right = cur->_right;
						}
						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						if (parent == nullptr)
							_root = cur->_left;
						else
						{
							if (cur == parent->_left)
								parent->_left = cur->_left;
							else
								parent->_right = cur->_left;
						}
						delete cur;
						return true;
					}
					else
					{
						Node* replace = cur->_right;
						Node* replaceparent = cur;
						while (replace->_left)
						{
							replaceparent = replace;
							replace = replace->_left;
						}
						cur->_key = replace->_key;
						if (replace == replaceparent->_left)
							replaceparent->_left = replace->_right;
						else
							replaceparent->_right = replace->_right;
						delete replace;
						return true;
					}
				}
			}

			return false;
		}

	private:
		void Inorder(Node* root)
		{
			if (root == nullptr)
				return;
			Inorder(root->_left);
			cout << root->_key << '_' << root->_value << ' ' << endl;
			Inorder(root->_right);
		}

	protected:

		Node* _root = nullptr;
	};
}

